package com.edushare.common.dao;

import java.util.HashMap;
import java.util.List;

import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.query.SelectQuery;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.ProfileAttrVals;
import com.edushare.common.vo.Users;

public class ProfileAttributesDAO extends CayenneBasedDAO {

	private static Log log = LogFactory.getLog(ProfileAttributesDAO.class);
	
	public ProfileAttributesDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}
	
	@SuppressWarnings("unchecked")
	public List<ProfileAttrVals> getAllProfileAttrValues(Users user){
		Expression exp = Expression.fromString("user = $user");
		HashMap hm = new HashMap();
		hm.put("user", user);
		SelectQuery query = new SelectQuery(ProfileAttrVals.class, exp.expWithParameters(hm));
		return getDataContext().performQuery(query);
	}
	
	@SuppressWarnings("unchecked")
	public List<ProfileAttrVals> getAttributeByName(Users user, String attributeName){
		Expression exp = Expression.fromString("user = $user and attrName = $attrName");
		HashMap hm = new HashMap();
		hm.put("user", user);
		hm.put("attrName", attributeName);
		SelectQuery query = new SelectQuery(ProfileAttrVals.class, exp.expWithParameters(hm));
		return getDataContext().performQuery(query);
	}
	
	public void createNewAttributeValue(Users u, ProfileAttrVals attribute, String attributeName){
		
		log.debug("\nDATA CONTEXT:\n user: " + u.getDataContext()+"\nAttr: "+attribute.getDataContext()+"\n getDataContext: "+getDataContext()+"\n" +
				"User: " + u+"\n Object Context: \nUser: "+ u.getObjectContext()+"\nattr: "+attribute.getObjectContext());
		
		attribute.setAttrName(attributeName);
		attribute.setUser(u);
		getDataContext().registerNewObject(attribute);
		
	}
}
